<?php
/**
 * @Author: liuhongbin
 * @Email: 514660094@qq.com
 * @Date: 2019-02-12 16:15:10
 * @Description:
 * @LastEditors: liuhongbin
 * @LastEditTime: xxx
 */

namespace addons\gs_developer\controller\addons;

use addons\gs_developer\model\Addons\AddonsModel;
use app\common\controller\AddonsController;
use app\common\model\addon\Addons;
use hiduoke\services\FormBuilder as Form;
use think\App;

class Addon extends AddonsController
{
    private AddonsModel $model;

    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->model = new AddonsModel();
    }

    public function index()
    {
        // 获取列表数据
        $res = $this->model->getPageList();
        return $this->success($res['count'], $res['data']);
    }

    public function delete($id)
    {
        $this->model->where('id', $id)->delete();
        return $this->success('删除成功');
    }

    public function getForm($id = 0)
    {
        $rule = array();
        $row = $this->model->findOrEmpty($id);//创建表单
        $form = Form::createForm("/gs_developer/addons.addon/save?id=" . $id)->setMethod("POST");
        //新增表单
        $rule[] = Form::input('title', '应用名', $row['title'] ?? '')->required();
        $types = (new Addons())->_types;
        $type_option = [];
        foreach ($types as $key => $value) {
            $type_option[] = array(
                'label' => $value,
                'value' => $key
            );
        }
        $rule[] = Form::select('type', '应用类型', $row['type'] ?? '')
            ->options($type_option);
        $rule[] = Form::input('author', '作者', $row['author'] ?? '')->required();
        $rule[] = Form::input('name', '标识', $row['name'] ?? '')->required();
        $rule[] = Form::dkImage('icon', '图标', $row['icon'] ?? '')->required();
        $rule[] = Form::input('desc', '描述', $row['desc'] ?? '')->required();
        $title = !$id ? "新增" : "编辑";
        $form->setTitle($title)->setRule($rule);
        return $this->success("", Form::getFormData($form));
    }

    public function save($id = 0)
    {
        $row = $this->model->findOrEmpty($id);
        $formData = getFormData();
        $row->save($formData);
        return $this->success("保存成功");
    }

    /**
     * 获取应用对应的数据库表
     * @param $aid
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getSqlTables($aid = 0)
    {
        if (!$aid) {
            return $this->error('请选择应用');
        }
        $addon = $this->model->where('id', $aid)->find();
        if (!$addon) {
            return $this->error('应用不存在');
        }
        $tables = $this->model->getAddonTables($addon['name']);
        return $this->success('', $tables);
    }

    public function getCreateTableSql($aid = 0)
    {
        if (!$aid) {
            return $this->error('请选择应用');
        }
        $addon = $this->model->where('id', $aid)->find();
        if (!$addon) {
            return $this->error('应用不存在');
        }
        $sql = $this->model->getCreateTableSql($addon['name']);
        return $this->success('ok', $sql);
    }

    public function getUpgradeSql($aid = 0)
    {
        $addon = $this->model->where('id', $aid)->find();
        if (!$addon) {
            return $this->error('应用不存在');
        }
        return $this->success('', ['sql' => $addon->upgrade_sql]);
    }

    public function saveUpgradeSql($aid, $sql)
    {
        $addon = $this->model->where('id', $aid)->find();
        if (!$addon) {
            return $this->error('应用不存在');
        }
        $addon->upgrade_sql = $sql;
        $addon->save();
        return $this->success('保存成功');
    }

    public function getAddonById($id)
    {
        $res = $this->model->where(['id' => $id])->find();
        return $this->success('', $res);
    }
}
